# coding:utf-8
'''
Created on 2011-12-11

@author: bobobo80
'''

from twython import Twython
import config
import time
import pushData

####################################
### global pushData.PUSHDATA
####################################

class twitterReq():
        def __init__(self):
                self.twitter = Twython(twitter_token=config.twitter_Consumer_key, 
                                       twitter_secret=config.twitter_Consumer_secret,
                                       oauth_token=config.twitter_Access_token, 
                                       oauth_token_secret=config.twitter_Access_token_secret)
                
        def GET_HomeTimeline(self):
                timeline = self.twitter.getHomeTimeline()
                linelist = []
                for tweet in timeline:
                        dic = {}
                        dic['id'] = tweet['id']
                        dic['user']['screen_name'] = tweet['user']['screen_name']
                        dic['text'] = tweet['text']
                        linelist.append(dic)
                return linelist
        
class mainControl():
        def __init__(self, looptime=60):
                self.looptime = looptime
                self.isFirst = True
        
        def start(self):
                self.serverInit()
                self.serverStart()
                self.checkloop()
                self.close()
                
        def serverInit(self):
                pass
        
        def serverStart(self):
                pass
        
        def checkloop(self):
                try:
                        while 1:
                                self.check_HomeTimeline()
                                time.sleep(self.looptime)
                finally:
                        self.close()
        
        def check_HomeTimeline(self):
                tw = twitterReq()
                TL_list = tw.GET_HomeTimeline()
                
                for tweet in TL_list:
                        if tweet['id'] in pushData.PUSHDATA.tweetIdPool:
                                pushData.PUSHDATA.homeTimeline.append(tweet)
                                pushData.PUSHDATA.tweetIdPool.append(tweet['id'])
        
        def close(self):
                pass

if __name__ == '__main__':
        tw = twitterReq()
        print 'start'
        list = tw.GET_HomeTimeline()
        print 'got it'
        print list
        